using UnityEngine;
using System.Collections;

public class OffensiveBuilding : MonoBehaviour 
{

	// Use this for initialization
   // public GameObject gameBullet = (GameObject)Resources.Load("/Prefabs/Weapons/playerBullet.prefab");
    public GameObject gameBullet;
    public float fireRate = .75f;
    private float mTimer = 0.0f;
    public static bool bulletAttack = false;
    public bool canShoot = false;
    void Start () 
    {
            //gameObject.SetActive(false);
	}
	// Update is called once per frame
    void Update()
    {
		if(PlayerPrefs.GetInt("AttackBoost") > 0)
		{
	//		fireRate=3.75f-(float)PlayerPrefs.GetInt("AttackBoost")*.25f+.25f;
			fireRate = 3f - (2.5f *(float)PlayerPrefs.GetInt("AttackBoost") /100f); 
			canShoot = true;
		}
        if (mTimer > fireRate && canShoot)
        {
            ShootBullets();
            mTimer = 0.0f;
        }
        mTimer += Time.deltaTime;
	}
    void ShootBullets()
    {
        int direction = 1;
        //float y = 90;
        //float z = 270;
        //if (transform.localRotation.eulerAngles.x > 270)
        //{
        //    y = 270f;
        //    z = 90f;
        //}
        if(/*transform.eulerAngles.x > 45 && transform.eulerAngles.x < 150*/ true)
        {
            //Weapon bullet  = ((GameObject)Instantiate(gameBullet, transform.position, Quaternion.Euler(-transform.eulerAngles.x,90,270))).GetComponent<Weapon>();
            GameObject bullet = Instantiate(gameBullet, new Vector3(transform.position.x, transform.position.y, 0.0f), Quaternion.Euler(transform.parent.localRotation.eulerAngles.x, transform.parent.localRotation.eulerAngles.y, transform.parent.localRotation.eulerAngles.z)) as GameObject;
            //GameObject bullet = Instantiate(gameBullet, transform.position, work) as GameObject;
            //bullet.transform.Translate(bullet.transform.forward * 500f);
      //      bullet.rigidbody.MovePosition(new Vector3(bullet.transform.position.x, bullet.transform.position.y, 0));
            bullet.rigidbody.mass = 1f;
            if (bullet.tag == "enemybullet")
            {
                direction = -1;
            }
            bullet.rigidbody.AddForce(direction * bullet.transform.forward * 500f);
			bullet.rigidbody.mass = .0001f;
        //    bullet.rigidbody.MovePosition(new Vector3(bullet.transform.position.x, bullet.transform.position.y, 0));
            //print("gun " + transform.eulerAngles.x + " rocket " + (-transform.eulerAngles.x) + " better? " + transform.localRotation.eulerAngles);
        }
        //bullet.currWeapon = Weapon.weaponState.Bullet;
        //Physics.IgnoreCollision(collider, bullet.collider, true);
        //Vector3 crossME = new Vector3(transform.rotation.x, transform.rotation.y, transform.rotation.z);
        //Vector3 crossME = -bullet.transform.forward;
        //bullet.fireDirection = crossME;
        //bullet.fireDirection = Vector3.Cross(transform.eulerAngles, crossME).normalized;
        //  bullet.fireDirection = Vector3.Cross(new Vector3(transform.eulerAngles.x, 90, 270), crossME).normalized;
    }
}
